package cn.webwheel.whtml;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Collection;

public class ModelSelectDlg extends JDialog implements ActionListener {

    public String selected;

    private JPanel contentPane;
    private JLabel label;
    private JPanel panel;

    public ModelSelectDlg(String path, Collection<String> models) {

        setModal(true);
        setTitle("选择模型");
        setAlwaysOnTop(true);
        setIconImage(MainFrame.icon2);

        setContentPane(contentPane);
        
        label.setText(label.getText().replace("\"\"", "\"" + path + "\""));

        GridLayout layout = new GridLayout((models.size() + 5) / 6, Math.min(models.size(), 6), 10, 10);
        panel.setLayout(layout);
        for (String model : models) {
            JButton bt = new JButton(model);
            panel.add(bt);
            bt.addActionListener(this);
        }

        ((JPanel) getContentPane()).registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        pack();
        setLocationRelativeTo(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        selected = e.getActionCommand();
        dispose();
    }
}
